//
//  LYlabelViewController.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/6/12.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "LYlabelViewController.h"
#import "JCTagListView.h"
#import "JCTableViewCell.h"
#import "JCCollectionViewTagFlowLayout.h"
#import "LYlabelModel.h"
#import "LYlabelListModel.h"

#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImage+AFNetworking.h>
#import <UIImageView+AFNetworking.h>

@interface LYlabelViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) IBOutlet JCTagListView *tagListView;

//  2

@property (nonatomic, strong) NSArray <LYlabelModel *>*labelModel;
@property (nonatomic, strong) LYlabelModel *label1 ;
@property (nonatomic, strong) LYlabelModel *label2;
@property (nonatomic, strong) LYlabelModel *label3;
@property (nonatomic, strong) NSArray <LYlabelListModel *> *ownLabel;


@property (nonatomic, copy) NSArray *allTags;
@property (nonatomic, copy) NSMutableArray *allTags2;
@property (nonatomic, strong) NSMutableArray *cellHeights;

@property (nonatomic, strong) JCCollectionViewTagFlowLayout *layout;
@property (nonatomic, weak) IBOutlet UITableView *tableView;

@end

@implementation LYlabelViewController

-(void)GetDidSelectLabelModel{
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:config];
    NSDictionary * paramDic = @{@"userid" : @"13653056525"};
    
    
    [manager GET:@"http://cute.fcloudclass.com/api.php/User/get_user_no_label" parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        self.labelModel = [LYlabelModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"label"]];

        self.label1 = self.labelModel[0];
        self.label2 = self.labelModel[1];
        self.label3 = self.labelModel[2];
        
        NSMutableArray *Marr1 = [[NSMutableArray alloc]init];
        NSMutableArray *Marr2 = [[NSMutableArray alloc]init];
        NSMutableArray *Marr3 = [[NSMutableArray alloc]init];
        
        for (int i = 0; i< self.label1.label_list.count; i++ ) {
            NSDictionary *dic  = self.label1.label_list[i];
            [Marr1 addObject:dic[@"label_name"]];
            
        }
        for (NSUInteger i = 0; i<self.label2.label_list.count; i ++) {
            NSDictionary *dic  = self.label2.label_list[i];
            [Marr2 addObject:dic[@"label_name"]];
            
        }
        for (NSUInteger i = 0; i<self.label3.label_list.count; i ++) {
            NSDictionary *dic  = self.label3.label_list[i];
            [Marr3 addObject:dic[@"label_name"]];
            
        }
        
             self.allTags = @[Marr1,Marr2,Marr3];
        
        for (NSArray *tags in self.allTags) {
            [self.cellHeights addObject:@([self.layout calculateContentHeight:tags])];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}
-(void)getLabelData{
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:config];
    NSDictionary * paramDic = @{@"userid" : @"13653056525"};
    
    
    [manager GET:@"http://cute.fcloudclass.com/api.php/User/get_user_label" parameters:paramDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        self.ownLabel = [LYlabelListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"user_label"]];
        
        for (int i= 0; i<self.ownLabel.count; i++) {
            [self.allTags2 addObject:self.ownLabel[i].label_name];
            
        }
        NSLog(@"%@",self.allTags2);
        self.tagListView.canSelectTags = YES;
        self.tagListView.tagSelectedTextColor = [UIColor blackColor];
        self.tagListView.tagCornerRadius = 5.0f;
        
        self.tagListView.tags  = self.allTags2;
        [self.tagListView.selectedTags addObjectsFromArray:self.allTags2];
        
        [self.tagListView setCompletionBlockWithSelected:^(NSInteger index) {
            NSLog(@"______%ld______", (long)index);
        }];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self GetDidSelectLabelModel];
    [self getLabelData];
    
    //2
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[JCTableViewCell class] forCellReuseIdentifier:@"ceId"];
    
    self.cellHeights = [NSMutableArray array];
    self.layout = [[JCCollectionViewTagFlowLayout alloc] init];
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.bounces = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.allTags.count == 0) {
        return 0;
    }
    return  self.allTags.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.cellHeights[indexPath.row] floatValue]+30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ceId" forIndexPath:indexPath];
    if (cell == NULL) {
        cell = [[JCTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ceId"];
    }
    cell.catName.text = self.labelModel[indexPath.row].cat_name;
    tableView.separatorStyle = NO;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.tags = self.allTags[indexPath.row];
    return cell;
}

-(bool)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}

@end
